//
//  SoapWsUtil.m
//  YaoChenPz
//
//  Created by 刘炳举 on 8/20/15.
//  Copyright (c) 2015 com.lbj.org. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "SoapWsUtil.h"
#import "GDataXMLNode.h"

@implementation SoapWsUtil

+(NSDictionary *)SoapCall:(NSString *)method  PostData:(NSString *)post {
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
    NSURL *url=[[NSURL alloc]initWithString:@"https://180.153.251.72:31751/WebServices4SLSM.asmx"];
    NSMutableURLRequest  *request=[[NSMutableURLRequest alloc]init];
    
    [request setTimeoutInterval: 10 ];
    [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
    [request setURL: url ] ;
    [request setHTTPMethod:@"POST"];
    [request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"http://tempuri.org/%@",method] forHTTPHeaderField:@"SOAPAction"];
    
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];
    
    
    NSError *err=nil;
    NSData *data=[NSURLConnection sendSynchronousRequest:request
                                       returningResponse:nil
                                                   error:&err];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
    GDataXMLElement *root = doc.rootElement;
    
    NSArray *array = [root elementsForName:@"soap:Body"];
    for (GDataXMLElement *ele in array) {
        NSLog(@"ele string %@",[ele stringValue]);
        NSString *retString = [ele stringValue];
        NSData *jsonData = [retString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary  *jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
        return jsonObject;
    
    }
    return nil;
}

+ (NSDictionary *)SoapInvoke:(NSString *)method params:(NSArray *)params
{
    NSMutableString * post = [[ NSMutableString alloc ] init ] ;
    
    [ post appendString:
     @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
     "<soap:Envelope "
     "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
     "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
     "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
     "<soapenv:Header/>\n"
     "<soapenv:Body>\n" ];
    
    [ post appendString:@"<ws:"];
    [ post appendString:method];
    [ post appendString:@">\n"];
    
    for (NSDictionary *dict in params)
    {
        for (int i=0; i<[[dict allKeys] count]; i++) {
            NSString *key = [[dict allKeys] objectAtIndex:i];
            NSString *value = [dict valueForKey:key];
            [ post appendString:@"<"];
            [ post appendString:key];
            [ post appendString:@">"];
            if( value != nil )
            {
                [ post appendString:value];
            }
            else
            {
                [ post appendString:@""];
            }
            
            [ post appendString:@"</"];
            [ post appendString:key];
            [ post appendString:@">\n"];
        }
    }
    
    [ post appendString:@"</ws:"];
    [ post appendString:method];
    [ post appendString:@">\n"];
    
    [ post appendString:
     @"</soapenv:Body>\n"
     "</soapenv:Envelope>\n"
     ];
    
    //    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"login" ofType:@"json"];
    //    NSString *test = [NSString stringWithContentsOfFile:filePath];
    NSLog(@"====================request=========================\n%@\n", post);
    
    return [self SoapCall:method PostData:post];
}


@end

